package com.baitap.Module;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;

public class EvtProcessor implements ActionListener {
	private ptGUI ownerGUI;
	private StringBuffer InputBuffer;  //
	private String preOperator;  		//tinh toan lan truoc
	private double preValue;
	private double memoryValue;
	private boolean newDigit;
	public EvtProcessor(ptGUI gui){
		ownerGUI = gui;		
	}
	
	//khởi tạo tính toán
	public void intPhepTinh(){
		InputBuffer = new StringBuffer();
		preOperator = "";
		preValue =0;
		memoryValue = 0;
		newDigit = (boolean) true;
	ownerGUI.setDisplayField("0");
		
	}

	public void actionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
		JButton bt = (JButton)evt.getSource();
		String value = bt.getText();
		String sa=ownerGUI.txta.getText();
		int a=0,b=0;
		if(value.equals("Thoát"))
		{
			int ret=JOptionPane.showConfirmDialog(null, "Muốn thoát hả?", "Thoát", JOptionPane.YES_NO_OPTION);
			if(ret==JOptionPane.YES_OPTION)
				System.exit(0);
		} else if(value.equals("Xóa"))
		{
			ownerGUI.txta.setText("");
			ownerGUI.txtb.setText("");
			ownerGUI.txtkq.setText("");
			ownerGUI.txta.requestFocus();
		} else {
		try
		{
			a=Integer.parseInt(sa);
		}
		catch(Exception ex)
		{
			JOptionPane.showMessageDialog(null, "Nhập sai định dạng!");
			ownerGUI.txta.selectAll();
			ownerGUI.txta.requestFocus();
			return;
		}
		String sb=ownerGUI.txtb.getText();
		try
		{
			b=Integer.parseInt(sb);
		}
		catch(Exception ex)
		{
			JOptionPane.showMessageDialog(null, "Nhập sai định dạng!");
			ownerGUI.txtb.selectAll();
			ownerGUI.txtb.requestFocus();
			return;
		}
		double kq=0;
		if(value.equals("Cộng"))
		{
			kq=(a+b);
		}
		else if(value.equals("Trừ"))
		{
			kq=(a-b);
		}
		else if(value.equals("Nhân"))
		{
			kq=(a*b).;
		}
		else
		{
			kq=a*1.0/b*1.0;
			
		}
		ownerGUI.txtkq.setText(kq+"");
		}
	}

}
